import { _decorator, Component, JsonAsset, Node } from 'cc';
import { ResourceUtil } from '../Utils/ResourceUtil';
import { HeroData, ItemData, PlayerData } from '../../Data';
import PrefsUtil from '../Utils/PrefsUtil';
import { Constant } from '../Datas/Constant';
import { PoolManager } from './PoolManager';
const { ccclass, property } = _decorator;

export class DataManager {
    static itemDatas: ItemData[] = null;
    static equippedItemDatas: ItemData[] = null;
    static heroDatas: HeroData[] = null;
    static playerData: PlayerData = null;

    static InitData(done: Function) {
        ResourceUtil.LoadJson("Data").then((ja: JsonAsset) => {
            let data = ja.json as any;
            DataManager.itemDatas = [];
            DataManager.heroDatas = [];
            DataManager.equippedItemDatas = [];

            for (let i = 0; i < data.Items.length; i++) {
                let e = data.Items[i];
                DataManager.itemDatas.push(new ItemData(e.Name, e.Index, e.Quality, e.Price, e.UpgratePieces, e.UpgratePrices, e.Damages, e.PushForces, e.DamageRadii));
            }

            for (let i = 0; i < data.Heroes.length; i++) {
                let e = data.Heroes[i];
                this.heroDatas.push(new HeroData(e.Index, e.Quality, e.Price));
            }

            //设置装备的卡
            let arr = PrefsUtil.GetItem(Constant.Key.EquipedIndexs);
            if (arr) {
                arr.split(",").forEach(e => DataManager.equippedItemDatas.push(DataManager.itemDatas.find(f => f.Index == Number(e))))
            } else {
                let count = 0;
                for (let i = 0; i < DataManager.itemDatas.length; i++) {
                    const e = DataManager.itemDatas[i];
                    if (e.Price == 0) {
                        DataManager.equippedItemDatas.push(e);
                        count++;
                    }

                    if (count >= 4) break;
                }


                let indexs = [];
                for (let i = 0; i < DataManager.equippedItemDatas.length; i++) {
                    const e = DataManager.equippedItemDatas[i];
                    indexs.push(e.Index);
                }

                PrefsUtil.SetItem(Constant.Key.EquipedIndexs, indexs);
            }

            //设置装备的角色
            this.GetSelectedHeroIndex();

            PoolManager.GenerateNodes("Prefabs/UI/Item", 30);
            PoolManager.GenerateNodes("Prefabs/UI/HeroItem", 10);

            done && done();
        });

        this.playerData = new PlayerData();

    }

    static AddEquippedItem(data: ItemData) {
        if (!this.equippedItemDatas.find(e => e == data)) {
            this.equippedItemDatas.push(data);
            this.SaveData();
        }
    }

    static SelectHero(index: number) {
        for (let i = 0; i < DataManager.heroDatas.length; i++) {
            const e = DataManager.heroDatas[i];
            if (e.IsUnlock) {
                e.IsSelected = e.Index == index;
            }
        }
    }

    static GetSelectedHeroIndex() {
        let index = -1;
        for (let i = 0; i < DataManager.heroDatas.length; i++) {
            const e = DataManager.heroDatas[i];
            if (e.IsUnlock && e.IsSelected) {
                if (index != -1) {
                    e.IsSelected = false;
                    continue;
                }

                index = e.Index;
            }
        }

        if (index == -1) {
            for (let i = 0; i < DataManager.heroDatas.length; i++) {
                const e = DataManager.heroDatas[i];
                if (e.IsUnlock) {
                    index = e.Index;
                    e.IsSelected = true;
                    break;
                }
            }
        }
        return index;
    }

    static SaveData() {
        let equipIndexs = [];

        for (let i = 0; i < this.equippedItemDatas.length; i++) {
            equipIndexs.push(this.equippedItemDatas[i].Index);
        }

        PrefsUtil.SetItem(Constant.Key.EquipedIndexs, equipIndexs);
    }

}